<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <el-input v-model="deptName" placeholder="请输入组织名称" clearable
                  size="small" prefix-icon="el-icon-search" @keyup.enter.native="handleSelectOrgCode"
        />
        <el-tree style="margin-top: 20px"
                 :data="deptOptions"
                 :filter-node-method="filterNode"
                 :expand-on-click-node="false"
                 ref="tree"
                 default-expand-all
                 @node-click="handleNodeClick"
        />
      </el-col>

      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" @submit.native.prevent>
          <el-form-item label="手机号" prop="account">
            <el-input
              v-model="queryParams.account"
              placeholder="请输入手机号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['log:appUserLogin:export']"
            >导出
            </el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="appLoginLogList" border @sort-change="handleSort">
          <el-table-column label="手机号" align="center" prop="account"/>
          <el-table-column label="手机类型" align="center" prop="sourceType">
            <template slot-scope="scope">
              <template v-if="scope.row.sourceType===1">
                IOS
              </template>
              <template v-else-if="scope.row.sourceType===2">
                安卓
              </template>
              <template v-else>
                未知
              </template>
            </template>
          </el-table-column>
          <el-table-column label="组织名称" align="center" prop="deptName"/>
          <el-table-column label="登录时间" align="center" prop="loginTime" width="180" sortable="custom"/>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { exportAppUserLoginLogList, listAppUserLoginLogList } from '@/api/system/logManage'
import { deptTreeSelect } from '@/api/system/dept'

export default {
  name: 'Message',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      appLoginLogList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: undefined,
        orgCode: undefined,
        sourceType: undefined,
        orderByLoginTime: undefined,
        deptId: undefined
      },
      // 表单参数
      form: {},
      // 部门树选项
      deptOptions: undefined,
      deptName: undefined
    }
  },
  created() {
    this.getList()
    this.getTreeselect()
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true

      if (!this.queryParams.deptId) {
        // 取出当前用户的组织ID
        this.queryParams.deptId = this.$store.getters.deptId
      }

      listAppUserLoginLogList(this.queryParams).then(response => {
        this.appLoginLogList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        account: undefined,
        orgCode: undefined,
        sourceId: undefined,
        sourceType: undefined,
        loginTime: undefined,
        createTime: undefined,
        modifyTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 查询部门下拉树结构 */
    getTreeselect() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
      })
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.orgCode = data.orgCode
      this.queryParams.deptId = data.id
      this.handleQuery()
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },

    handleSelectOrgCode() {
      this.$refs.tree.filter(this.deptName)
      this.handleQuery()
    },

    /**
     * 点击排序按钮
     * @param event
     */
    handleSort(event) {
      //先清空之前的排序
      this.queryParams.orderByLoginTime = undefined

      // 转换排序的字符串, 将其转为sql中的形式
      let orderType = null
      if (event.order) {
        orderType = event.order === 'ascending' ? 'asc' : 'desc'
      }

      //先清空之前的排序
      this.queryParams.orderByLoginTime = orderType

      this.getList()
    },

    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return exportAppUserLoginLogList(this.queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>
